package edu.gatech.cs2340.aperturescience.backend.category;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import edu.gatech.cs2340.aperturescience.backend.TodoSQLiteHelper;
import edu.gatech.cs2340.aperturescience.backend.task.Task;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.MergeCursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.util.Log;

/**
 * SQLCategoryDataSource manages the Categories database
 * @author Kevin R Lacey
 *
 */
public class SQLCategoryDataSource implements CategoryDataSource {
	private SQLiteDatabase database;
	private TodoSQLiteHelper dbHelper;
	private int userid;

	public SQLCategoryDataSource(Context context) {
		dbHelper = new TodoSQLiteHelper(context);
	}

	/**
	 * Opens the database.
	 */
	public void open() throws SQLException
	{
		database = dbHelper.getWritableDatabase();
	}

	/**
	 * Closes the database.
	 */
	public void close() {
		if (database != null)
			database.close();
	}

	/**
	 * updateCategory initializes a new Category for a user
	 * @param userid - the user
	 * @param category - the new Category
	 */
	public void updateCategory(int userid, String category) {
		ContentValues values = new ContentValues();
		values.put(dbHelper.COLUMN_CATEGORYUSER, String.valueOf(userid));
		values.put(dbHelper.COLUMN_CATEGORY, category);
		database.insert(dbHelper.TABLE_CATEGORY, null, values);
	}


	/**
	 * getAllCategories returns a list of Categories that have been saved by the user
	 * @param userid - the user
	 * @return - a list of Categories
	 */
	public List<Category> getAllCategories(int userid) {
		this.userid = userid;
		Cursor c = database.rawQuery("SELECT category FROM categories WHERE id=?", new String[] { "" + userid });
		
		return getCategories(c);
	}

	/**
	 * getCategories is a helper method that returns a list of Categories
	 * @param cursor - a database query
	 * @return - a list of categories
	 */
	private List<Category> getCategories(Cursor cursor)
	{
		Category completedCategory = null;
		ArrayList<Category> categories = new ArrayList<Category>();
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Category category = getCategory(cursor);
			
			if(!category.getCategory().equals("Completed"))
			{
				categories.add(category);
			}
			else
			{
				completedCategory = category;
			}
			
			cursor.moveToNext();
		}
		
		if(completedCategory != null)
		{
			categories.add(completedCategory);
		}
		
		return categories;
	}
	
	/**
	 * isEmpty returns a boolean value indicated whether any Categories have been generated
	 * @param userid - the user
	 * @return - a boolean value indicating the existence of any Categories
	 */
	public boolean isEmpty(int userid)
	{
		boolean isEmpty = false;
		
		this.userid = userid;
		Cursor c = database.rawQuery(
				"SELECT category FROM categories WHERE id=?", // order by
																// alphabet and
																// date added?
				new String[] { "" + userid });
		
		if(getCategories(c).isEmpty())
		{
			isEmpty = true;
		}
		
		return isEmpty;
	}

	/** getCategory is a helper method that returns a Category if it exists
	 * @param cursor - a database query
	 * @return - a Category
	 */
	private Category getCategory(Cursor cursor) {
		String category = cursor.getString(0);
		Category task = new Category(userid, category);
		return task;
	}


	/**
	 * deleteCategory deletes a specified Category
	 * @param userid - the user
	 * @param category - the specific Category to be deleted
	 */
	public void deleteCategory(int userid, String category) {
		database.delete(dbHelper.TABLE_CATEGORY, "userid=? AND category=?",
				new String[] { "" + userid, category });
	}

}
